package com.lie.prepare.multi_thread.mypractice.mypool;

import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import static com.lie.prepare.util.Print.*;

/**
 * Created by lie on 2018/4/11.
 */

class Task implements Runnable{

    private static int counter =0;
    private final int id = counter++;

    private Random random = new Random(47);

    public void run() {
        try {
            TimeUnit.MILLISECONDS.sleep(random.nextInt(800));
            print("id = "+ id + "任务run完毕");
        } catch (InterruptedException e) {
            print("task sleep InterruptedException");
        }
    }

    @Override
    public String toString() {
        return "Task{" +
                "id=" + id +
                '}';
    }
}

public class PoolDemo {
    static Task t1 = new Task();
    static Task t2 = new Task();
    static Task t3 = new Task();
    static Task t4 = new Task();

    public static void main(String[] args){
        myPool();
//        thatPool();
    }

    private static void thatPool() {
        ExecutorService exec = Executors.newCachedThreadPool();
        exec.execute(t1);
        exec.execute(t2);
        exec.execute(t3);
        exec.execute(t4);
        exec.shutdown();
    }

    private static void myPool() {
        MyPool mypool = new MyPool();
        mypool.execute(t1);
        mypool.execute(t2);
        mypool.execute(t3);
        mypool.execute(t4);
    }
}
